

CalendarService = Packages.com.google.gdata.client.calendar.CalendarService;
gdata           = Packages.com.google.gdata.data;
CalendarEntry   = Packages.com.google.gdata.data.calendar.CalendarEntry;
PlainTextConstruct = Packages.com.google.gdata.data.PlainTextConstruct;
TimeZoneProperty   = Packages.com.google.gdata.data.calendar.TimeZoneProperty;
HiddenProperty     = Packages.com.google.gdata.data.calendar.HiddenProperty;
ColorProperty      = Packages.com.google.gdata.data.calendar.ColorProperty;
Where              = Packages.com.google.gdata.data.extensions.Where;


function main( ui ) { 

    var json = ui.getQueryParameters().getFirst("json");
    var o = new JSONObject( json );
    var EMAIL_JSON_KEY="EMAIL";
    
    var service = new CalendarService("demo-calendar-1");
   
    var username = "soulsoftware2010@gmail.com";
    var password = "2010soulsoftware";
    var userCalendarTitle= o.getString(EMAIL_JSON_KEY);
  
    service.setUserCredentials(username, password);
    try {
          // Create the calendar
             var calendar = new CalendarEntry();
               
             calendar.setTitle(new PlainTextConstruct(userCalendarTitle));
             calendar.setSummary(new PlainTextConstruct("This calendar contains the practice schedule and game times."));
             calendar.setTimeZone(new TimeZoneProperty ("Europe/Rome"));
             calendar.setHidden(HiddenProperty.FALSE);
             calendar.setColor(new ColorProperty("#2952A3"));
             calendar.addLocation(new Where("","","Rome"));
               
               // Insert the calendar
             var postUrl = new java.net.URL("https://www.google.com/calendar/feeds/default/owncalendars/full");
             var returnedCalendar = service.insert(postUrl, calendar);
             return  returnedCalendar.getId();
    }
    catch( e ) {
    
        return e;
    }
    
    return "OK";

}